{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import lightgbm as lgb\n",
    "import datetime\n",
    "import math\n",
    "import time\n",
    "from sklearn.preprocessing import LabelEncoder,StandardScaler\n",
    "import gc\n",
    "import xlearn as xl"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 7377418 entries, 0 to 7377417\n",
      "Data columns (total 38 columns):\n",
      "artist_composer             float64\n",
      "artist_composer_lyricist    float64\n",
      "artist_lyricist             float64\n",
      "artist_name                 object\n",
      "bd                          int64\n",
      "city                        int64\n",
      "composer                    object\n",
      "expiration_date             int64\n",
      "expiration_month            int64\n",
      "expiration_year             int64\n",
      "gender                      object\n",
      "genre_ids                   object\n",
      "isrc_name                   object\n",
      "language                    float64\n",
      "lyricist                    object\n",
      "membership_days             int64\n",
      "msno                        object\n",
      "registered_via              int64\n",
      "registration_date           int64\n",
      "registration_month          int64\n",
      "registration_year           int64\n",
      "song_id                     object\n",
      "song_length                 float64\n",
      "song_year                   float64\n",
      "source_screen_name          object\n",
      "source_system_tab           object\n",
      "source_type                 object\n",
      "genre_ids_count             int64\n",
      "lyricists_count             int64\n",
      "composer_count              int64\n",
      "artist_count                int64\n",
      "is_featured                 int64\n",
      "song_lang_boolean           int64\n",
      "smaller_song                int64\n",
      "count_song_played           int64\n",
      "count_artist_played         int64\n",
      "count_user_played           int64\n",
      "target                      int64\n",
      "dtypes: float64(6), int64(21), object(11)\n",
      "memory usage: 2.1+ GB\n"
     ]
    }
   ],
   "source": [
    "data_path = './target_data/'\n",
    "train = pd.read_csv(data_path + 'train.csv')\n",
    "test = pd.read_csv(data_path + 'test.csv')\n",
    "train.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "target = train.pop('target')\n",
    "test_id = test.pop('id')\n",
    "n_train = train.shape[0]\n",
    "n_test = test.shape[0]\n",
    "df = train.append(test)\n",
    "del train,test\n",
    "gc.collect()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 特征分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "cat_col = ['artist_composer','artist_composer_lyricist','artist_lyricist','artist_name',\n",
    "          'city','composer','expiration_month','gender','genre_ids','isrc_name',\n",
    "           'language','msno','registered_via','registration_month','song_id',\n",
    "           'source_screen_name','source_system_tab','source_type','is_featured',\n",
    "           'song_lang_boolean','smaller_song']\n",
    "num_col = ['bd','expiration_date','expiration_year','registration_date','registration_year',\n",
    "          'song_length','genre_ids_count','lyricists_count','composer_count','artist_count',\n",
    "          'count_song_played','count_artist_played','count_user_played','membership_days',\n",
    "          'song_year']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数值特征处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[df.membership_days <= 0] = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "log处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "col_log = ['song_length','lyricists_count','composer_count','artist_count',\n",
    "           'count_song_played','count_artist_played','count_user_played']\n",
    "df[col_log] = np.log1p(df[col_log])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "标准化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "col_sta = ['bd','expiration_date','expiration_year','registration_date','registration_year',\n",
    "          'genre_ids_count','membership_days','song_year']\n",
    "df[col_sta] = StandardScaler().fit_transform(df[col_sta])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "类别特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "artist_composer\n",
      "artist_composer_lyricist\n",
      "artist_lyricist\n",
      "artist_name\n",
      "city\n",
      "composer\n",
      "expiration_month\n",
      "gender\n",
      "genre_ids\n",
      "isrc_name\n",
      "language\n",
      "msno\n",
      "registered_via\n",
      "registration_month\n",
      "song_id\n",
      "source_screen_name\n",
      "source_system_tab\n",
      "source_type\n",
      "is_featured\n",
      "song_lang_boolean\n",
      "smaller_song\n"
     ]
    }
   ],
   "source": [
    "for col in cat_col:\n",
    "    print(col)\n",
    "    df[col] = df[col].astype('str')\n",
    "    df[col] = LabelEncoder().fit_transform(df[col])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>artist_composer</th>\n",
       "      <th>artist_composer_lyricist</th>\n",
       "      <th>artist_lyricist</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>bd</th>\n",
       "      <th>city</th>\n",
       "      <th>composer</th>\n",
       "      <th>expiration_date</th>\n",
       "      <th>expiration_month</th>\n",
       "      <th>expiration_year</th>\n",
       "      <th>...</th>\n",
       "      <th>genre_ids_count</th>\n",
       "      <th>lyricists_count</th>\n",
       "      <th>composer_count</th>\n",
       "      <th>artist_count</th>\n",
       "      <th>is_featured</th>\n",
       "      <th>song_lang_boolean</th>\n",
       "      <th>smaller_song</th>\n",
       "      <th>count_song_played</th>\n",
       "      <th>count_artist_played</th>\n",
       "      <th>count_user_played</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>3781</td>\n",
       "      <td>-0.538183</td>\n",
       "      <td>0</td>\n",
       "      <td>16654</td>\n",
       "      <td>-1.163289</td>\n",
       "      <td>1</td>\n",
       "      <td>0.027182</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.128471</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.098612</td>\n",
       "      <td>0.693147</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>5.375278</td>\n",
       "      <td>7.039660</td>\n",
       "      <td>8.614683</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>36844</td>\n",
       "      <td>-0.270515</td>\n",
       "      <td>4</td>\n",
       "      <td>74275</td>\n",
       "      <td>-0.504392</td>\n",
       "      <td>11</td>\n",
       "      <td>0.027182</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.128471</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>3.044522</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.693147</td>\n",
       "      <td>12.633058</td>\n",
       "      <td>6.434547</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>24585</td>\n",
       "      <td>-0.270515</td>\n",
       "      <td>4</td>\n",
       "      <td>51537</td>\n",
       "      <td>-0.504392</td>\n",
       "      <td>11</td>\n",
       "      <td>0.027182</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.128471</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.693147</td>\n",
       "      <td>0.693147</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.609438</td>\n",
       "      <td>5.669881</td>\n",
       "      <td>6.434547</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>31633</td>\n",
       "      <td>-0.270515</td>\n",
       "      <td>4</td>\n",
       "      <td>41989</td>\n",
       "      <td>-0.504392</td>\n",
       "      <td>11</td>\n",
       "      <td>0.027182</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.128471</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.693147</td>\n",
       "      <td>0.693147</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.693147</td>\n",
       "      <td>0.693147</td>\n",
       "      <td>6.434547</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5185</td>\n",
       "      <td>-0.538183</td>\n",
       "      <td>0</td>\n",
       "      <td>9702</td>\n",
       "      <td>-1.163289</td>\n",
       "      <td>1</td>\n",
       "      <td>0.027182</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.128471</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.386294</td>\n",
       "      <td>0.693147</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>6.023448</td>\n",
       "      <td>6.059123</td>\n",
       "      <td>8.614683</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 37 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   artist_composer  artist_composer_lyricist  artist_lyricist  artist_name  \\\n",
       "0                0                         0                0         3781   \n",
       "1                0                         0                0        36844   \n",
       "2                0                         0                0        24585   \n",
       "3                0                         0                0        31633   \n",
       "4                1                         0                0         5185   \n",
       "\n",
       "         bd  city  composer  expiration_date  expiration_month  \\\n",
       "0 -0.538183     0     16654        -1.163289                 1   \n",
       "1 -0.270515     4     74275        -0.504392                11   \n",
       "2 -0.270515     4     51537        -0.504392                11   \n",
       "3 -0.270515     4     41989        -0.504392                11   \n",
       "4 -0.538183     0      9702        -1.163289                 1   \n",
       "\n",
       "   expiration_year  ...  genre_ids_count  lyricists_count  composer_count  \\\n",
       "0         0.027182  ...        -0.128471              0.0        1.098612   \n",
       "1         0.027182  ...        -0.128471              0.0        0.000000   \n",
       "2         0.027182  ...        -0.128471              0.0        0.693147   \n",
       "3         0.027182  ...        -0.128471              0.0        0.693147   \n",
       "4         0.027182  ...        -0.128471              0.0        1.386294   \n",
       "\n",
       "   artist_count is_featured  song_lang_boolean  smaller_song  \\\n",
       "0      0.693147           0                  0             1   \n",
       "1      3.044522           0                  0             0   \n",
       "2      0.693147           0                  0             1   \n",
       "3      0.693147           0                  0             0   \n",
       "4      0.693147           0                  0             1   \n",
       "\n",
       "   count_song_played  count_artist_played  count_user_played  \n",
       "0           5.375278             7.039660           8.614683  \n",
       "1           0.693147            12.633058           6.434547  \n",
       "2           1.609438             5.669881           6.434547  \n",
       "3           0.693147             0.693147           6.434547  \n",
       "4           6.023448             6.059123           8.614683  \n",
       "\n",
       "[5 rows x 37 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 划分数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "valid_index = [5000000,6000000,7000000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# input\n",
    "fp_train_1 = \"./target_data/fm/train_ffm1.txt\"\n",
    "fp_valid_1 = \"./target_data/fm/valid_ffm1.txt\"\n",
    "fp_train_2 = \"./target_data/fm/train_ffm2.txt\"\n",
    "fp_valid_2 = \"./target_data/fm/valid_ffm2.txt\"\n",
    "fp_train_3 = \"./target_data/fm/train_ffm3.txt\"\n",
    "fp_valid_3 = \"./target_data/fm/valid_ffm3.txt\"\n",
    "fp_train = \"./target_data/fm/train_ffm.txt\"\n",
    "fp_test = \"./target_data/fm/test_ffm.txt\"\n",
    "\n",
    "# output\n",
    "fp_model_fm = \"./target_data/fm/model_fm.out\"\n",
    "fp_model_ffm = \"./target_data/fm/model_ffm.out\"\n",
    "fp_pred_fm  = \"./target_data/fm/output_fm.txt\"\n",
    "fp_pred_ffm = \"./target_data/fm/output_ffm.txt\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df.reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.drop('index',inplace=True,axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 转换数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "def conver_to_libffm(df,cat_col,num_col,features,target=target,n_train=n_train):\n",
    "    \n",
    "    ts = time.time()\n",
    "    with open(fp_train_1,'w') as t1,\\\n",
    "        open(fp_train_2,'w') as t2,\\\n",
    "        open(fp_train_3,'w') as t3,\\\n",
    "        open(fp_valid_1,'w') as v1,\\\n",
    "        open(fp_valid_2,'w') as v2,\\\n",
    "        open(fp_valid_3,'w') as v3,\\\n",
    "        open(fp_train,'w') as file_train,\\\n",
    "        open(fp_test,'w') as file_test:\n",
    "        \n",
    "        for n,row in df.iterrows():\n",
    "            datastr = ''\n",
    "            if n >= n_train:\n",
    "                datastr += '-1'\n",
    "            else :\n",
    "                datastr += str(target[n])\n",
    "            \n",
    "            for i,col in enumerate(features):\n",
    "                if col in cat_col:\n",
    "                    datastr += ' ' + str(i) + \":\" + \"%d\"%row[col] + \":1\"\n",
    "                elif col in num_col:\n",
    "                    datastr += ' ' + str(i) + \":\" +str(i) + \":\" + \"%.5f\"%row[col]\n",
    "            \n",
    "            datastr += '\\n'\n",
    "            \n",
    "            if n < n_train:\n",
    "                file_train.write(datastr)\n",
    "                if n < valid_index[0]:\n",
    "                    t1.write(datastr)\n",
    "                else:\n",
    "                    v1.write(datastr)\n",
    "                if n < valid_index[1]:\n",
    "                    t2.write(datastr)\n",
    "                else:\n",
    "                    v2.write(datastr)\n",
    "                if n < valid_index[2]:\n",
    "                    t3.write(datastr)\n",
    "                else:\n",
    "                    v3.write(datastr)\n",
    "            else:\n",
    "                file_test.write(datastr)\n",
    "                \n",
    "            if n % 500000 == 0:\n",
    "                print(n,'data has handled...........%.1fs'%(time.time()-ts))\n",
    "                ts = time.time()\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 data has handled...........8.0s\n",
      "500000 data has handled...........193.2s\n",
      "1000000 data has handled...........178.6s\n",
      "1500000 data has handled...........179.4s\n",
      "2000000 data has handled...........178.4s\n",
      "2500000 data has handled...........179.6s\n",
      "3000000 data has handled...........177.6s\n",
      "3500000 data has handled...........180.6s\n",
      "4000000 data has handled...........181.6s\n",
      "4500000 data has handled...........182.2s\n",
      "5000000 data has handled...........204.1s\n",
      "5500000 data has handled...........177.8s\n",
      "6000000 data has handled...........176.8s\n",
      "6500000 data has handled...........180.8s\n",
      "7000000 data has handled...........178.9s\n",
      "7500000 data has handled...........174.6s\n",
      "8000000 data has handled...........159.3s\n",
      "8500000 data has handled...........163.7s\n",
      "9000000 data has handled...........159.4s\n",
      "9500000 data has handled...........160.7s\n"
     ]
    }
   ],
   "source": [
    "conver_to_libffm(df,cat_col,num_col,df.columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "47"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gc.collect()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 参数调整"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "1168.1s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts = time.time()\n",
    "\n",
    "param = {\n",
    "         'task': 'binary',\n",
    "         'k': 30,\n",
    "         'lr': 0.02,\n",
    "         'lambda': 0.01,\n",
    "         'epoch': 20,\n",
    "         'opt': 'adagrad',\n",
    "         'metric':'auc'\n",
    "}\n",
    "\n",
    "print('1')\n",
    "model_fm = xl.create_fm()\n",
    "model_fm.setTrain(fp_train_1)\n",
    "model_fm.setValidate(fp_valid_1)\n",
    "model_fm.setSigmoid()\n",
    "model_fm.fit(param, fp_model_fm)\n",
    "print('2')\n",
    "model_fm = xl.create_fm()\n",
    "model_fm.setTrain(fp_train_2)\n",
    "model_fm.setValidate(fp_valid_2)\n",
    "model_fm.setSigmoid()\n",
    "model_fm.fit(param, fp_model_fm)\n",
    "print('3')\n",
    "model_fm = xl.create_fm()\n",
    "model_fm.setTrain(fp_train_3)\n",
    "model_fm.setValidate(fp_valid_3)\n",
    "model_fm.setSigmoid()\n",
    "model_fm.fit(param, fp_model_fm)\n",
    "\n",
    "ts1 = time.time()\n",
    "print(\"%.1fs\"%(ts1 - ts))\n",
    "gc.collect()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 训练全体数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "param = {\n",
    "         'task': 'binary',\n",
    "         'k': 30,\n",
    "         'lr': 0.02,\n",
    "         'lambda': 0.01,\n",
    "         'epoch': 100,\n",
    "         'opt': 'adagrad',\n",
    "         'metric':'auc'\n",
    "}\n",
    "model_fm = xl.create_fm()\n",
    "model_fm.setTrain(fp_train)\n",
    "model_fm.setSigmoid()\n",
    "model_fm.fit(param,fp_model_fm)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_fm.setTest(fp_train)\n",
    "pred1 = model_fm.predict(fp_model_fm,'./target_data/fm_train.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_fm.setTest(fp_test)\n",
    "pred2 = model_fm.predict(fp_model_fm,'./target_data/fm_text.csv')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### FFM模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "param = {\n",
    "         'task': 'binary',\n",
    "         'k': 30,\n",
    "         'lr': 0.02,\n",
    "         'lambda': 0.01,\n",
    "         'epoch': 100,\n",
    "         'opt': 'adagrad',\n",
    "         'metric':'auc'\n",
    "}\n",
    "model_ffm = xl.create_ffm()\n",
    "model_ffm.setTrain(fp_train)\n",
    "model_ffm.setSigmoid()\n",
    "model_ffm.fit(param,fp_model_ffm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_ffm = xl.create_ffm()\n",
    "model_ffm.setTest(fp_train)\n",
    "model_ffm.predict(fp_model_ffm,'./target_data/ffm_train.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_ffm.setTest(fp_test)\n",
    "model_ffm.predict(fp_model_ffm,'./target_data/ffm_test.csv')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 融合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "param = {\n",
    "         'task': 'binary',\n",
    "         'k': 30,\n",
    "         'lr': 0.02,\n",
    "         'lambda': 0.01,\n",
    "         'epoch': 100,\n",
    "         'opt': 'adagrad',\n",
    "         'metric':'auc'\n",
    "}\n",
    "model_fm = xl.create_fm()\n",
    "model_fm.setTrain(fp_train_2)\n",
    "model_fm.setSigmoid()\n",
    "model_fm.fit(param,fp_model_fm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_fm.setTest(fp_valid_2)\n",
    "pred1 = model_fm.predict(fp_model_fm,'./target_data/fm_train.csv')\n",
    "model_fm.setTest(fp_test)\n",
    "pred2 = model_fm.predict(fp_model_fm,'./target_data/fm_test.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_ffm = xl.create_ffm()\n",
    "model_ffm.setTrain(fp_train_2)\n",
    "model_ffm.setSigmoid()\n",
    "model_ffm.fit(param,fp_model_ffm)\n",
    "\n",
    "model_ffm.setTest(fp_valid_2)\n",
    "model_ffm.predict(fp_model_ffm,'./target_data/ffm_train.csv')\n",
    "model_ffm.setTest(fp_test)\n",
    "model_ffm.predict(fp_model_ffm,'./target_data/ffm_test.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
